package com.ucampus.dao;

import com.ucampus.entity.ManagerUser;
import com.ucampus.entity.userinfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface userinfoMapper {
    // 查询所有用户
    @Select("SELECT * FROM userinfo")
    List<userinfo> getAllUsers();

    // 根据ID查询用户
    @Select("SELECT * FROM userinfo WHERE id = #{id}")
    userinfo getUserById(long id);

    // 新增用户
    @Insert("INSERT INTO userinfo (username, userAvatar, userGender, userBirthday, userNation, userPhone, userEmail, userAddress, userLive, userBio) " +
            "VALUES (#{username}, #{userAvatar}, #{userGender}, #{userBirthday}, #{userNation}, #{userPhone}, #{userEmail}, #{userAddress}, #{userLive}, #{userBio})")
    int addUser(userinfo user);

    // 更新用户信息
    @Update({
            "<script>",
            "UPDATE userinfo",
            "<set>",
            "<if test='username != null'>username = #{username},</if>",
            "<if test='userAvatar != null'>userAvatar = #{userAvatar},</if>",
            "<if test='userGender != null'>userGender = #{userGender},</if>",
            "<if test='userBirthday != null'>userBirthday = #{userBirthday},</if>",
            "<if test='userNation != null'>userNation = #{userNation},</if>",
            "<if test='userPhone != null'>userPhone = #{userPhone},</if>",
            "<if test='userEmail != null'>userEmail = #{userEmail},</if>",
            "<if test='userAddress != null'>userAddress = #{userAddress},</if>",
            "<if test='userLive != null'>userLive = #{userLive},</if>",
            "<if test='userBio != null'>userBio = #{userBio},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateUser(userinfo user);

    // 根据 ID 删除用户
    @Delete("DELETE FROM userinfo WHERE id = #{id}")
    int deleteUser(long id);

    //管理员查询所有用户信息
    @Select("select userinfo.id,userinfo.username,user.createtime,user.status from userinfo inner join user on userinfo.id=user.id")
    List<ManagerUser> getUsers();

    //管理员根据用户昵称模糊查询信息
    @Select("select userinfo.id,userinfo.username,user.createtime,user.status from userinfo inner join user on userinfo.id=user.id where userinfo.username like concat('%',#{username},'%')")
    List<ManagerUser> getUsersByName(String username);

    //管理员修改用户账号状态
    @Update("<script>"+
            "update user " +
            "<set>" +
            "status=case when status='可用' then '不可用'" +
            "when status='不可用' then '可用' end" +
            "</set>" +
            "where id in" +
            "<foreach collection='list' item='id' open='(' close=')' separator=','>#{id}</foreach>"+
            "</script>")
    int updateStatus(List<Integer> list);

    //管理员删除用户
    @Delete("<script>"+
            "delete userinfo,user from userinfo join user on userinfo.id=user.id " +
            "where userinfo.id in" +
            "<foreach collection='list' item='id' open='(' close=')' separator=','>#{id}</foreach>"+
            "</script>")
    int delete(List<Integer> list);
}
